package cn.lingyangwl.agile.member.controller.app;

import cn.lingyangwl.agile.common.security.utils.SecurityUtils;
import cn.lingyangwl.agile.member.model.rqrs.address.AddressResp;
import cn.lingyangwl.agile.member.model.rqrs.address.AddressSaveReq;
import cn.lingyangwl.agile.member.model.rqrs.address.AddressSaveThirdReq;
import cn.lingyangwl.agile.member.service.MemberAddressService;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.framework.validation.group.InsertGroup;
import cn.lingyangwl.framework.validation.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 会员订单地址
 * 
 * @author shenguangyang
 */
@Api(tags = "会员订单地址")
@RestController
@RequestMapping("/app/member/address")
public class AppMemberAddressController {
    @Resource
    private MemberAddressService addressService;

    @ApiOperation(value = "查询会员订单地址")
    @GetMapping("/list")
    @RamResource(action = "appMember:address:query", name = "app-查询会员地址", type = ActionType.READ)
    public Resp<List<AddressResp>> listAddress() {
        return Resp.ok(addressService.listAddress());
    }

    @ApiOperation(value = "通过id查找会员订单地址")
    @GetMapping("/get")
    @RamResource(action = "appMember:address:query", name = "app-查询会员地址", type = ActionType.READ)
    public Resp<AddressResp> getAddress(@RequestParam(value = "id") Long id) {
        return Resp.ok(addressService.getAddress(SecurityUtils.getUserId(), id));
    }
  
    @ApiOperation(value = "保存会员订单地址")
    @PostMapping("/save")
    @RamResource(action = "appMember:address:save", name = "app-保存会员订单地址", type = ActionType.WRITE)
    public Resp<?> saveAddress(@Validated(InsertGroup.class) @RequestBody AddressSaveReq req) {
        req.setUserId(SecurityUtils.getUserId());
        return Resp.ok(addressService.saveAddress(req));
    }
  
    @ApiOperation(value = "更新会员订单地址")
    @PostMapping("/update")
    @RamResource(action = "appMember:address:update", name = "app-更新会员地址", type = ActionType.WRITE)
    public Resp<?> updateAddress(@Validated(UpdateGroup.class) @RequestBody AddressSaveReq req) {
        req.setUserId(SecurityUtils.getUserId());
        return Resp.ok(addressService.updateAddress(req));
    }

    @ApiOperation(value = "删除会员订单地址")
    @DeleteMapping("/delete")
    @RamResource(action = "appMember:address:delete", name = "app-删除会员地址", type = ActionType.WRITE)
    public Resp<?> deleteAddress(@RequestParam(value = "id") Long id) {
        return Resp.ok(addressService.deleteAddress(id));
    }

    @ApiOperation(value = "设置默认会员订单地址")
    @PostMapping("/setDefault")
    @RamResource(action = "appMember:address:setDefault", name = "app-设置默认会员订单地址", type = ActionType.WRITE)
    public Resp<?> setDefault(@RequestParam(value = "id") Long id) {
        addressService.setDefault(id);
        return Resp.ok();
    }

    @ApiOperation(value = "保存第三方的地址")
    @PostMapping("/saveThreeParties")
    @RamResource(action = "appMember:address:saveThreeParties", name = "app-保存第三方的地址", type = ActionType.WRITE)
    public Resp<?> saveThreeParties(@RequestBody @Validated AddressSaveThirdReq req) {
        addressService.saveThreeParties(req);
        return Resp.ok();
    }

}
